package com.blog.serlvet;

import com.blog.bean.Blog;
import com.blog.bean.User;
import com.blog.dao.BlogDao;
import com.blog.util.LoginUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class IndexPageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = LoginUtil.checkPageLogin(session, resp);
        if (user == null) {
            return;
        }

        //  微博数据
        BlogDao blogDao = new BlogDao();
        // 最新微博
        try {
            List<Blog> latestBlogs = blogDao.selectLatestBlog(null, 10, user.getId());
//            System.out.println(latestBlogs);
            req.setAttribute("latestBlogs", latestBlogs);
        } catch (SQLException e) {
            e.printStackTrace();
            resp.sendError(500);
        }
        // 最热微博
        try {
            List<Blog> hotBlogs = blogDao.selectHotBlog(null, 10, user.getId());
//            System.out.println(hotBlogs);
            req.setAttribute("hotBlogs", hotBlogs);
        } catch (SQLException e) {
            e.printStackTrace();
            resp.sendError(500);
        }
        // 我的微博
        try {
            List<Blog> myBlogs = blogDao.selectMyBlog(null, 10, user.getId());
//            System.out.println(myBlogs);
            req.setAttribute("myBlogs", myBlogs);
        } catch (SQLException e) {
            e.printStackTrace();
            resp.sendError(500);
        }

        req.getRequestDispatcher("WEB-INF/blog.jsp").forward(req, resp);
    }
}
